package util;

import java.util.Calendar ;
import java.util.Date ;

/**
 * @author idan
 */
public class DateTools
{
	/** format for date which includes the time. */
	public static final String GLOBAL_DATE_FORMAT = "yyyy-MM-dd-HH:mm:ss" ;

	/**
	 * @param date date augend.
	 * @param field field (from Calendar) to which to add.
	 * @param value amount of that field to add.
	 * @return the sum date.
	 */
	public static Date add(final Date date, final int field, final int value)
	{
		Calendar c = Calendar.getInstance() ;
		c.setTime(date) ;
		c.add(field, value) ;
		return c.getTime() ;
	}

	/**
	 * @param date date to set.
	 * @param field field (from Calendar) to set.
	 * @return value of the date in the given field.
	 */
	public static int get(final Date date, final int field)
	{
		Calendar c = Calendar.getInstance() ;
		c.setTime(date) ;
		return c.get(field) ;
	}

	/**
	 * note that this function returns a new date, since Date is immutable.
	 * @param date date to set.
	 * @param field field (from Calendar) to set.
	 * @param value value of that field.
	 * @return a new date with the given field set to the given value.
	 */
	public static Date set(final Date date, final int field, final int value)
	{
		Calendar c = Calendar.getInstance() ;
		c.setTime(date) ;
		c.set(field, value) ;
		return c.getTime() ;
	}
}
